//
//  RechargeView.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/8.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "RechargeView.h"
#import "RelationCollectionViewCell.h"
@interface RechargeView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)UICollectionView * collectionView;
@property (nonatomic, strong)NSMutableArray * cellData;
@end

@implementation RechargeView(networking)
- (void)rechargeList{
    [[AFAppDotNetAPIClient sharedClient] POST:@"api/rechargeList" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"msg"] isEqualToString:@"success"]) {
            [self.cellData removeAllObjects];
            [self.cellData addObject:responseObject[@"list"][0]];
            [self.collectionView reloadData];
        }else{
            [self makeToast:responseObject[@"msg"] duration:1 position:CSToastPositionCenter];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self makeToast:@"操作失败" duration:1 position:CSToastPositionCenter];
    }];
}

@end

@implementation RechargeView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        //背景
        UIButton * bgBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
        bgBtn.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [bgBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:bgBtn];
        //控件搭建
        [self setUpViews];
        
        [self rechargeList];
    }return self;
}

- (void)setUpViews{
    //    圆角背景
    UIView * cornerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 214/WIDTH_6_SCALE - BOTTOM_HEIGHT, SCREEN_WIDTH, 214/WIDTH_6_SCALE)];
    [self addSubview:cornerView];
    cornerView.backgroundColor = [UIColor whiteColor];
    //得到view的遮罩路径
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cornerView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = cornerView.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    cornerView.layer.mask = maskLayer;
    //白色背景
    UIView * whiteBGView = [[UIView alloc] init];
    whiteBGView.backgroundColor = [UIColor clearColor];
    [self addSubview:whiteBGView];
    //标题
    UILabel * titleLab = [[UILabel alloc] init];
    titleLab.text = @"选择充值金额";
    titleLab.textColor = [UIColor blackColor];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = DEF_FontSize_14;
    titleLab.backgroundColor = [UIColor clearColor];
    [whiteBGView addSubview:titleLab];
    //取消
    UIButton * cancelBtn = [[UIButton alloc] init];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:getColor(@"afafaf") forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = DEF_FontSize_12;
    [cancelBtn setBackgroundColor:[UIColor clearColor]];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [whiteBGView addSubview:cancelBtn];
    //建议
    UILabel * suggestLab = [[UILabel alloc] init];
    suggestLab.text = @"建议充值30元以上，以免余额不足失去权益";
    suggestLab.textColor = getColor(@"afafaf");
    suggestLab.textAlignment = NSTextAlignmentLeft;
    suggestLab.font = DEF_FontSize_14;
    [whiteBGView addSubview:suggestLab];
    //分割线
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = getColor(@"eeeeee");
    [whiteBGView addSubview:lineView];
    //collectionView
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(((SCREEN_WIDTH - 43/WIDTH_6_SCALE*2) - 20/WIDTH_6_SCALE)/3, 40/WIDTH_6_SCALE);
    layout.sectionInset = UIEdgeInsetsMake(17/WIDTH_6_SCALE, 43/WIDTH_6_SCALE, 35/WIDTH_6_SCALE, 43/WIDTH_6_SCALE);
    //两行之间的距离
    layout.minimumLineSpacing = 10/WIDTH_6_SCALE;
    layout.minimumInteritemSpacing = 10/WIDTH_6_SCALE;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectNull collectionViewLayout:layout];
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[RelationCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([RelationCollectionViewCell class])];
    [whiteBGView addSubview:self.collectionView];
    //masonry
    __weak typeof (self)weakSelf = self;
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.height.mas_offset(214/WIDTH_6_SCALE);
        make.bottom.equalTo(weakSelf.mas_bottom).mas_offset(-BOTTOM_HEIGHT);
    }];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(whiteBGView);
        make.height.mas_equalTo(50/WIDTH_6_SCALE);
    }];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(whiteBGView);
        make.width.mas_offset(59/WIDTH_6_SCALE);
        make.height.equalTo(titleLab);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(whiteBGView);
        make.height.mas_offset(1/WIDTH_6_SCALE);
        make.top.equalTo(cancelBtn.mas_bottom);
    }];
    [suggestLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).mas_offset(18/WIDTH_6_SCALE);
        make.left.equalTo(whiteBGView.mas_left).mas_offset(16/WIDTH_6_SCALE);
        make.right.equalTo(whiteBGView.mas_right).mas_offset(-16/WIDTH_6_SCALE);
        make.height.mas_offset(14/WIDTH_6_SCALE);
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(whiteBGView);
        make.top.equalTo(suggestLab.mas_bottom);
    }];
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
//        _cellData = @[@{@"content":@"9"},
//                      @{@"content":@"30"},
//                      @{@"content":@"50"},
//                      @{@"content":@"100"},
//                      @{@"content":@"150"},
//                      @{@"content":@"300"},].mutableCopy;
        _cellData = @[].mutableCopy;
    }return _cellData;
}
#pragma mark - action
- (void)cancelBtnClick{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
#pragma mark - collectionDelegage和collectionDataSource
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RelationCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([RelationCollectionViewCell class]) forIndexPath:indexPath];
    cell.rechargeModel = self.cellData[indexPath.row];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.cellData.count;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString * contentStr = [NSString stringWithFormat:@"%@", [self.cellData[indexPath.row] objectForKey:@"num"]];
    [self cancelBtnClick];//去掉背景
    if (self.rechargeStringBlock) {
        self.rechargeStringBlock(contentStr);
    }
}

@end
